/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rdfparser.bean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import rdfparser.db.DBConnection;

/**
 *
 * @author Amendra Shrestha
 */
public class PostBean {

    private int PostID;
    private String post;
    private String type;
    private String title;
    private String user;
    private String seeAlso;
    private String maker;
    private String makerSeeAlso;
    private String created;
    private String content;
    private String replyOfPost;
    private String replyOfseeAlso;
    Connection con = null;

    public PostBean() throws SQLException {
        con = DBConnection.getDBConnection();
        con.setAutoCommit(false);
    }

    public int getPostID() {
        return PostID;
    }

    public void setPostID(int PostID) {
        this.PostID = PostID;
    }

    public String getReplyOfseeAlso() {
        return replyOfseeAlso;
    }

    public void setReplyOfseeAlso(String replyOfseeAlso) {
        this.replyOfseeAlso = replyOfseeAlso;
    }

    public String getReplyOfPost() {
        return replyOfPost;
    }

    public void setReplyOfPost(String replyOfPost) {
        this.replyOfPost = replyOfPost;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getCreated() {
        return created;
    }

    public void setCreated(String created) {
        this.created = created;
    }

    public String getMaker() {
        return maker;
    }

    public void setMaker(String maker) {
        this.maker = maker;
    }

    public String getMakerSeeAlso() {
        return makerSeeAlso;
    }

    public void setMakerSeeAlso(String makerSeeAlso) {
        this.makerSeeAlso = makerSeeAlso;
    }

    public String getPost() {
        return post;

    }

    public void setPost(String post) {
        this.post = post;
    }

    public String getSeeAlso() {
        return seeAlso;
    }

    public void setSeeAlso(String seeAlso) {
        this.seeAlso = seeAlso;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public void addPosts() throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;

        String InsertPostSql = "INSERT INTO tbl_posts_98 (Post, Type, "
                + "Title, User, seeAlso, Maker, MakerSeeAlso, Created, "
                + "Content, Reply_Of_Post, Reply_Of_SeeAlso, PostID, MakerID) "
                + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        try {
            ps = con.prepareStatement(InsertPostSql);
            String tempPost = getPost();

            if (tempPost != null) {
                String tempMaker = getMaker();
                String tempgetSeeAlso = getSeeAlso();
                String tempgetReplyOfPost = getReplyOfPost();

                ps.setString(1, tempPost);
                ps.setString(2, getType());
                ps.setString(3, getTitle());
                if (getUser() != null) {
                    ps.setString(4, getUser());
                } else {
                    ps.setString(4, "Guest");
                }
                ps.setString(5, tempgetSeeAlso);
                if (tempMaker != null) {
                    ps.setString(6, tempMaker);
                } else {
                    ps.setString(6, "Guest");
                }
                ps.setString(7, getMakerSeeAlso());
                ps.setString(8, getCreated());
                ps.setString(9, getContent().trim());
                ps.setString(10, getReplyOfPost());
                ps.setString(11, tempgetReplyOfPost);
                ps.setInt(12, Integer.parseInt(tempPost.substring(tempPost.indexOf('=') + 1)));

                if (tempMaker != null) {
                    ps.setInt(13, Integer.parseInt(tempMaker.substring(tempMaker.indexOf('=') + 1,
                            tempMaker.indexOf('#'))));
                } else {
                    ps.setString(13, null);
                }
                /*if (tempgetReplyOfPost != null) {
                ps.setInt(14, Integer.parseInt(tempgetReplyOfPost.substring(tempgetReplyOfPost.indexOf('=') + 1)));
                } else {
                ps.setString(14, null);
                }*/

                ps.executeUpdate();

                rs = con.prepareStatement("SELECT LAST_INSERT_ID()").executeQuery();
                if (rs.next()) {
                    setPostID(rs.getInt(1));
                }
                con.commit();
            }
        } catch (SQLException ex) {
            try {
                con.rollback();// rollback on error
            } catch (SQLException ex1) {
            }
            System.err.println("Error while adding data...." + ex.getMessage());
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
                if (ps != null) {
                    ps.close();
                }
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException ex) {
            }
        }
    }
}
